﻿using System;
using System.Web;

public class WebDAVAnonymousAuthorization : IHttpModule
{
    public WebDAVAnonymousAuthorization()
    {
    }

    public string ModuleName
    {
        get { return "WebDAVAnonymousAuthorization"; }
    }

    // In the Init function, register for HttpApplication 
    // events by adding your handlers.
    public void Init(HttpApplication application)
    {
        application.BeginRequest +=
            (new EventHandler(this.Application_BeginRequest));
    }

    private void Application_BeginRequest(Object source,
                                          EventArgs e)
    {
        string userName = "two\\kolbin";
        string password = "123456789";
        string concat = userName + ":" + password;
        string base64string = EncodeTo64(concat);
        var application = (HttpApplication)source;
        var context = application.Context;
        context.Request.Headers.Add("Authorization", "Basic " + base64string);

    }
    static public string EncodeTo64(string toEncode)
    {
        byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
        string returnValue = Convert.ToBase64String(toEncodeAsBytes);
        return returnValue;
    }

    public void Dispose() { }
}


